$(document).ready(
		function() {
			//alert(document.cookie);
			var openId = getQueryParamValue("openId");
			var result = getCookie("WECHAT_USER_OPENID");
			if(result==null){
			  //alert('openId 不存在!');
			  setCookie("WECHAT_USER_OPENID",openId);
			  //location.reload();
			  //alert(getCookie("WECHAT_USER_OPENID"));
			}else{
				//alert(result);
			}
			
			var rootPath = getRootPath();
			//alert("rootPath:"+rootPath);
			/*去掉iphone手机滑动默认行为*/
//			$('body').on('touchmove', function (event) {
//			    event.preventDefault();
//			});
			$.ajax({
				headers: {'Cookie' : document.cookie },
				method : 'get',
				url : rootPath+'/customer/api/v1/fans',
				error:function(XMLHttpRequest, ajaxOptions, thrownError){
					//alert("数据刷新失败");
				},
				success : function(resultData){
					if(resultData.length==0){
						//不是会员
					}else{
						//是会员
						alert("不可以重复注册！");
						window.location = rootPath + "/views/hpp.html";
					}
				}
			});
			$.ajax({
				headers: {'Cookie' : document.cookie },
				method : 'get',
				url : rootPath+'/customer/api/v1/potentialInfo',
				error:function(XMLHttpRequest, ajaxOptions, thrownError){
					//alert("数据刷新失败");
				},
				success : function(resultData){
					$.each(resultData,function(memberInfoIndex,memberInfoObj){
							//$("#userId").val(memberInfoObj.userId)
							$("#potentialUserId").val(memberInfoObj.potentialUserId);
							$("#identifyingCode").val(memberInfoObj.identifyingCode);
							$("#identifyingCodeDate").val(memberInfoObj.identifyingCodeDate);
						
						});
				}
			});

})
//注册按钮
function sure(){	
	
	if($("input[type='checkbox']").is(':checked')==true){
		var carOwnerDto = {
			"userName" : $("#userName").val(),
			"phone" : $("#phone").val(),
			"identifyingCode" : $("#yan").val(),
			"potentialUserId" : $("#potentialUserId").val(),
			"randomcode" : $("#randomcode").val(),
		};
		
		$.ajax({
			headers: {'Cookie' : document.cookie },
			type : "POST",
			url : rootPath + "/customer/api/v1/insertCarOwner",
			dataType : "json",
			contentType : "application/json",
			data : JSON.stringify(carOwnerDto),
			success : function(resultDate) {
				$.ajax({
					headers: {'Cookie' : document.cookie },
				    type:"GET", 
				    url:rootPath+"/customer/api/v1/memberInfo", 
				    dataType:"json",      
				    async: false,
				    contentType:"application/json",               
				    data:null, 
				    success:function(data){ 
				    	$.each(data,function(Index,Obj){
				    		$("#userId").val(Obj.userId);
				    		});
				    },
				    error:function(data){
				    	//alert("数据刷新失败，请联系管理员！");
				    	
				    }
				 });
				
	        	var data=eval(resultDate);
	        	alert(data.resultMsg);
	        	if(data.resultCode=='1'){
					window.location = rootPath + "/views/homepage/Authentication.html?userId="+$("#userId").val();
	        	}else if(data.resultCode==2){
	        		$("#random").removeClass("hidden");
	        		createCode(data.result);
	        		$('#sign').removeClass('adclass');
	        	}else{
	        		$('#sign').removeClass('adclass');
	        	}
			},
			error : function(data) {
				//alert("注册失败！");
				
			}
		});
	}else{
		alert("请阅读并同意《广汽三菱会员服务协议》");
	}
}
var wait=60;
function time(o) {
  if (wait == 0) {
   o.removeAttribute("disabled");   
   o.value="获取验证码";
   wait = 60;
  } else { 
 
   o.setAttribute("disabled", true);
   o.value="重新发送(" + wait + ")";
   wait--;
   setTimeout(function() {
    time(o)
   },
   1000)
  }
 }
//点击获取验证码
function GetRandom(o){
	var phone = $("#phone").val();
	if(!$("#phone").val().match(/^1[34578]\d{9}$/)){
		alert("请输入正确的手机号码！");
		$("#phone").focus();
	}else{
	//生成验证码
	var Num=""; 
	for(var i=0;i<6;i++) 
	{ 
		Num+=Math.floor(Math.random()*10); 
	}
	$("#identifyingCode").val(Num);
	var identifyingCodeDto = {"identifyingCode":$("#identifyingCode").val(),
							"potentialUserId":$("#potentialUserId").val(),
							"phone":$("#phone").val()
	}
	$.ajax({
		headers: {'Cookie' : document.cookie },
		type : "POST",
		url : rootPath + "/customer/api/v1/updateCode",
		dataType : "json",
		contentType : "application/json",
		data : JSON.stringify(identifyingCodeDto),
		success : function(data) {
			//alert("更新成功！");
			//location.reload();
		},
		error : function(data) {
			//alert("更新失败！");
		}
	});
	time(o);
  }
}


function checkSubmitMobil() {
// 姓名验证	
$('#sign').addClass('adclass')
if ($("#userName").val() == "") {
	alert("姓名不能为空！");
	//$("#moileMsg").html("<font color='red'>手机号码不能为空！</font>");
	$("#userName").focus();
	$('#sign').removeClass('adclass')
	return false;
}
if (!$("#userName").val().match(/[\u4E00-\u9FA5]{2,4}/)) {
	alert("请输入正确的姓名！");
	//$("#moileMsg").html("<font color='red'>手机号码格式不正确！请重新输入！</font>");
	$("#userName").focus();
	$('#sign').removeClass('adclass')
	return false;
}

//jquery验证手机号码
if ($("#phone").val() == "") {
alert("手机号码不能为空！");
//$("#moileMsg").html("<font color='red'>手机号码不能为空！</font>");
$("#phone").focus();
$('#sign').removeClass('adclass')
return false;
}
if (!$("#phone").val().match(/^1[34578]\d{9}$/)) {
alert("手机号码格式不正确！");
//$("#moileMsg").html("<font color='red'>手机号码格式不正确！请重新输入！</font>");
$("#phone").focus();
$('#sign').removeClass('adclass')
return false;
}
if ($("#yan").val() == "") {
	alert("验证码不能为空！");
	//$("#moileMsg").html("<font color='red'>手机号码不能为空！</font>");
	$("#phone").focus();
	$('#sign').removeClass('adclass')
	return false;
	}
sure();
return true;
} 
function getCookie(cookie_name) {
	var allcookies = document.cookie;
	var cookie_pos = allcookies.indexOf(cookie_name);

	// 如果找到了索引，就代表cookie存在，
	// 反之，就说明不存在。
	if (cookie_pos != -1) {
		// 把cookie_pos放在值的开始，只要给值加1即可。
		cookie_pos += cookie_name.length + 1;
		var cookie_end = allcookies.indexOf(";", cookie_pos);

		if (cookie_end == -1) {
			cookie_end = allcookies.length;
		}

		var value = unescape(allcookies.substring(cookie_pos, cookie_end));
	}

	return value;
}
function setCookie(name,value)
{
    var Days = 1;
    var exp  = new Date(); 
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/";
	//alert(document.cookie);
}
function createCode(obj) {
    var code = obj;
    var checkCode = document.getElementById("checkCode");
    if (checkCode) 
    {
        checkCode.className = "code";
        checkCode.innerHTML = code;
    }
}